﻿using System.Configuration;
using System.Linq;
using System.Web.Mvc;
using Ruici.BLL;
using RuiciWebSite.Models;
using System.Collections.Generic;
using RuiciWebSite.Common;

namespace RuiciWebSite.Controllers
{
    public class ExpertDetailController : Controller
    {
        private readonly DocInfoBll _docInfoBll;
        public static string docImageUrl = ConfigurationManager.AppSettings["ImageUrl"];
        

        public ExpertDetailController()
        {
            _docInfoBll = new DocInfoBll();
        }
        // GET: ExpertDetail
        public ActionResult Index(string doctorid = "")
        {
            return View();
        }
        /// <summary>
        /// 获取医生详情
        /// </summary>
        /// <param name="expertDetailViewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetDoctorInfo(ExpertDetailViewModel expertDetailViewModel)
        {
            ExpertDetailViewModel doctorinfo = new ExpertDetailViewModel();
            if (!string.IsNullOrWhiteSpace(expertDetailViewModel.Id))
            {
                var docinfoget = _docInfoBll.GetDoctor(expertDetailViewModel.Id);
                doctorinfo.Id = docinfoget.id;
                doctorinfo.AffiliatedHospital = docinfoget.affiliatedHospital;
                doctorinfo.Name = docinfoget.name;
                doctorinfo.DeptName = docinfoget.deptName;
                doctorinfo.DepName2 = docinfoget.depName2;
                doctorinfo.DeptID = docinfoget.deptID;
                doctorinfo.TitleName = docinfoget.titleName;
                doctorinfo.SkillIn = string.IsNullOrWhiteSpace(docinfoget.skillIn) ? "" : docinfoget.skillIn;
                doctorinfo.Image = docImageUrl + docinfoget.image;
                doctorinfo.OrgName = docinfoget.orgName;
                doctorinfo.DocInfo = string.IsNullOrWhiteSpace(docinfoget.docInfo) ? "" :docinfoget.docInfo;
                doctorinfo.SkillInText = docinfoget.skillIn;
                doctorinfo.DocInfoText = docinfoget.docInfo;
            }
                
            return Json(doctorinfo, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取博士文章
        /// </summary>
        /// <param name="expertDetailViewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetArticListsForDoctor(ExpertDetailViewModel expertDetailViewModel)
        {
            List<DoctorArticleInfo> articList = new List<DoctorArticleInfo>();
            int totalcount;
            if (!string.IsNullOrWhiteSpace(expertDetailViewModel.Id))
            {
                var articLists = _docInfoBll.GetArticleListForDoctor("video", 0, 100, 1, "", expertDetailViewModel.Id, out totalcount);
                articList = articLists.Select(m => new DoctorArticleInfo
                {
                    id = m.id,
                    channel_id = m.channel_id,
                    category_id = m.category_id,
                    call_index = m.call_index,
                    title = m.title,
                    link_url = m.link_url,
                    img_url = docImageUrl + m.img_url,
                    seo_title = m.seo_title,
                    seo_keywords = m.seo_keywords,
                    seo_description = m.seo_description,
                    zhaiyao = m.zhaiyao,
                    content = m.content,
                    sort_id = m.sort_id,
                    click = m.click,
                    status = m.status,
                    is_msg = m.is_msg,
                    is_top = m.is_top,
                    is_red = m.is_red,
                    is_hot = m.is_hot,
                    is_slide = m.is_slide,
                    is_sys = m.is_sys,
                    update_time = m.update_time,
                    add_time = m.add_time,
                    videoSrc = m.videoSrc,
                    fileSrc = m.fileSrc,
                    user_name = m.user_name
                }).ToList();
            }
            return Json(articList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 相关疾病大全
        /// </summary>
        /// <param name="expertDetailViewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetDiseaseListsForByDeptName(ExpertDetailPageInfoViewModel pageInfo)
        {   
            List<DoctorArticleInfo> articList = new List<DoctorArticleInfo>();
            int totalcount;
            if (!string.IsNullOrWhiteSpace(pageInfo.DeptID))
            {
                var articLists = _docInfoBll.GetArticleListForDoctor("disease", 0, 100, 1, "", pageInfo.DeptID, out totalcount);
                articList = articLists.Select(m => new DoctorArticleInfo
                {
                    id = m.id,
                    channel_id = m.channel_id,
                    category_id = m.category_id,
                    call_index = m.call_index,
                    title = m.title,
                    link_url = m.link_url,
                    img_url = docImageUrl + m.img_url,
                    seo_title = m.seo_title,
                    seo_keywords = m.seo_keywords,
                    seo_description = m.seo_description,
                    zhaiyao = string.IsNullOrWhiteSpace(m.zhaiyao)?"": m.zhaiyao.Replace("&amp;","").Replace("&emsp;", "").Replace("emsp;", ""),
                    content = m.content,
                    sort_id = m.sort_id,
                    click = m.click,
                    status = m.status,
                    is_msg = m.is_msg,
                    is_top = m.is_top,
                    is_red = m.is_red,
                    is_hot = m.is_hot,
                    is_slide = m.is_slide,
                    is_sys = m.is_sys,
                    update_time = m.update_time,
                    add_time = m.add_time,
                    videoSrc = m.videoSrc,
                    fileSrc = m.fileSrc,
                    user_name = m.user_name
                }).ToList();
            }

            int iTotalCount = articList.Count;

            pageInfo.totalCount = iTotalCount;
            var pagecount = (iTotalCount + pageInfo.pageSize - 1) / pageInfo.pageSize;
            pageInfo.pageCount = pagecount;
            if (iTotalCount > 0)
            {
                var list = articList.OrderBy(o => o.add_time).Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            else
            {
                var list = articList.Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            if (pageInfo != null)
                return Json(pageInfo, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有相关疾病大全"), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 相关指南共识
        /// </summary>
        /// <param name="expertDetailViewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetGuideListsForByDeptName(ExpertDetailPageInfoViewModel pageInfo)
        {
            List<DoctorArticleInfo> articList = new List<DoctorArticleInfo>();
            int totalcount;
            if (!string.IsNullOrWhiteSpace(pageInfo.DeptID))
            {
                var articLists = _docInfoBll.GetArticleListForDoctor("guide", 0, 100, 1, "", pageInfo.DeptID, out totalcount);
                articList = articLists.Select(m => new DoctorArticleInfo
                {
                    id = m.id,
                    channel_id = m.channel_id,
                    category_id = m.category_id,
                    call_index = m.call_index,
                    title = m.title,
                    link_url = m.link_url,
                    img_url = docImageUrl + m.img_url,
                    seo_title = m.seo_title,
                    seo_keywords = m.seo_keywords,
                    seo_description = m.seo_description,
                    zhaiyao = m.zhaiyao,
                    content = m.content,
                    sort_id = m.sort_id,
                    click = m.click,
                    status = m.status,
                    is_msg = m.is_msg,
                    is_top = m.is_top,
                    is_red = m.is_red,
                    is_hot = m.is_hot,
                    is_slide = m.is_slide,
                    is_sys = m.is_sys,
                    update_time = m.update_time,
                    add_time = m.add_time,
                    videoSrc = m.videoSrc,
                    fileSrc = m.fileSrc,
                    user_name = m.user_name
                }).ToList();
            }

            int iTotalCount = articList.Count;

            pageInfo.totalCount = iTotalCount;
            var pagecount = (iTotalCount + pageInfo.pageSize - 1) / pageInfo.pageSize;
            pageInfo.pageCount = pagecount;
            if (iTotalCount > 0)
            {
                var list = articList.OrderBy(o => o.add_time).Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            else
            {
                var list = articList.Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            if (pageInfo != null)
                return Json(pageInfo, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有相关指南共识"), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 病案分享
        /// </summary>
        /// <param name="expertDetailViewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetCaseListsForByDeptName(ExpertDetailPageInfoViewModel pageInfo)
        {
            List<DoctorArticleInfo> articList = new List<DoctorArticleInfo>();
            int totalcount;
            if (!string.IsNullOrWhiteSpace(pageInfo.DeptID))
            {
                var articLists = _docInfoBll.GetArticleListForDoctor("case", 0, 100, 1, "", pageInfo.DeptID, out totalcount);
                articList = articLists.Select(m => new DoctorArticleInfo
                {
                    id = m.id,
                    channel_id = m.channel_id,
                    category_id = m.category_id,
                    call_index = m.call_index,
                    title = m.title,
                    link_url = m.link_url,
                    img_url = docImageUrl + m.img_url,
                    seo_title = m.seo_title,
                    seo_keywords = m.seo_keywords,
                    seo_description = m.seo_description,
                    zhaiyao = m.zhaiyao,
                    content = m.content,
                    sort_id = m.sort_id,
                    click = m.click,
                    status = m.status,
                    is_msg = m.is_msg,
                    is_top = m.is_top,
                    is_red = m.is_red,
                    is_hot = m.is_hot,
                    is_slide = m.is_slide,
                    is_sys = m.is_sys,
                    update_time = m.update_time,
                    add_time = m.add_time,
                    videoSrc = m.videoSrc,
                    fileSrc = m.fileSrc,
                    user_name = m.user_name
                }).ToList();
            }

            int iTotalCount = articList.Count;

            pageInfo.totalCount = iTotalCount;
            var pagecount = (iTotalCount + pageInfo.pageSize - 1) / pageInfo.pageSize;
            pageInfo.pageCount = pagecount;
            if (iTotalCount > 0)
            {
                var list = articList.OrderBy(o => o.add_time).Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            else
            {
                var list = articList.Skip(pageInfo.pageCurrent * pageInfo.pageSize - pageInfo.pageSize).Take(pageInfo.pageSize).ToList();
                pageInfo.curPageRecord = list;
            }
            if (pageInfo != null)
                return Json(pageInfo, JsonRequestBehavior.AllowGet);

            return Json(new ReturnCode(-1, "没有相关病案分享"), JsonRequestBehavior.AllowGet);            
        }              
    }
}